import numpy as np
import pandas as pd

df = pd.read_csv(
    "./data/11数据.csv",
    encoding="gbk",
    header=None,
    dtype={'col0':float,'col1': str, 'col2': float,'col3':str,'col4':int,'col5':int,'col6':int,'col7':int,'col8':int,'col9':int},
    na_values=['NaN']
)
print(df[2].dtype)
print("*"*50)

#删除全是nan的一行
df.dropna(how="all",inplace=True)
print(df)
print("*"*50)


#将第0列的数据类型设置为整数
# df[df.columns[0]] = df[df.columns[0]].astype(int)
# print(df)
# print("*"*50)

#设置行名
# df.set_index(df.columns[0],inplace=True)
# print(df)
# print("*"*50)

#删除第0列
df.drop(df.columns[0],axis=1,inplace=True)
print(df)
print("*"*50)

#添加列名
# data = df.columns.tolist()
# df.loc[-1] = data
# df.index = df.index+1
# df.sort_index(inplace=True)
df.columns=["name","age","weight","man_bust","man_waist","man_buttocks","woman_bust","woman_waist","woman_buttocks"]
print(df)
print("*"*50)

#在年龄列填补缺失值
df["age"].fillna(df["age"].mean(),inplace=True)
df["age"] = df["age"].round().astype(int)
print(df)
print(df["age"].dtype)
print("*"*50)

#将姓和名分开存储
df["first_name"] = df["name"].str.split(" ", expand=True)[0]
df["last_name"] = df["name"].str.split(" " , expand=True)[1]
df.drop("name",axis=1,inplace=True)
print(df)
print("*"*50)

#统一体重的单位
w_lbs = df["weight"].str.contains("lbs").fillna(False)
# print(w_lbs)
# print(df[w_lbs])
for i , row in df[w_lbs].iterrows():
    weight = int(float(row["weight"][0:-3])/2.2)
    df.at[i,"weight"] = "{}kgs".format(weight)
df['weight'].fillna("0kgs",inplace=True)
print(df)

#删除相同的数据
df.drop_duplicates(["first_name","last_name"],inplace=True)
print(df)